//
//  GameLevelViewController.m
//  linkingGame
//
//  Created by zw wu on 15/4/5.
//  Copyright (c) 2015年 com.jinWu. All rights reserved.
//

#import "GameLevelViewController.h"
#import "GameMainViewController.h"
#import "GameLevelMainView.h"
#import "GameLevelTipView.h"

@interface GameLevelViewController () <GameLevelMainViewDelegate>

@property (nonatomic,retain) GameLevelTipView *gameLevelTipView;
@property (nonatomic,retain) GameLevelMainView *gameLevelMainView;

@end

@implementation GameLevelViewController

- (BOOL)prefersStatusBarHidden
{
    return YES;//隐藏为YES，显示为NO
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self resetAllSubViewFrame];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        [self prefersStatusBarHidden];
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    
    self.view.backgroundColor = [UIColor grayColor];
    
    GameLevelTipView *gameLevelTipView = [[GameLevelTipView alloc]init];
    [self.view addSubview:gameLevelTipView];
    self.gameLevelTipView = gameLevelTipView;
    [gameLevelTipView release];
    
    gameLevelTipView.backgroundColor = [UIColor orangeColor];
    
    [gameLevelTipView.backBtn addTarget:self action:@selector(goToBack) forControlEvents:UIControlEventTouchUpInside];
    
    GameLevelMainView *gameLevelMainView = [[GameLevelMainView alloc]init];
    gameLevelMainView.gameDelegate = self;
    [self.view addSubview:gameLevelMainView];
    self.gameLevelMainView = gameLevelMainView;
    [gameLevelMainView release];
    
    gameLevelMainView.backgroundColor = [UIColor lightGrayColor];
    
    [self.gameLevelMainView resetLevelCount:30];
    [self.gameLevelMainView resetUnlockLevelCount:1];
    
    [self.gameLevelTipView resetGameMode:self.gameMode];
    [self.gameLevelTipView resetGameStars:60 withAll:100];
}

- (void)resetAllSubViewFrame
{
    CGSize mainSize = self.view.bounds.size;
    CGRect gameLevelTipViewRect,gameLevelMainViewRect;
    if (isPhone())
    {
        gameLevelTipViewRect = CGRectMake(0, 0, mainSize.width, 88);
        gameLevelMainViewRect = CGRectMake(0, 88, mainSize.width, mainSize.height - 88);
    }
    else
    {
        if (isLandscape())
        {
            gameLevelTipViewRect = CGRectMake(0, 0, mainSize.width, 120);
            gameLevelMainViewRect = CGRectMake(0, 120, mainSize.width, mainSize.height - 120);
        }
        else
        {
            gameLevelTipViewRect = CGRectMake(0, 0, mainSize.width, 180);
            gameLevelMainViewRect = CGRectMake(0, 180, mainSize.width, mainSize.height - 180);
        }
    }
    
    if (!CGRectEqualToRect(gameLevelTipViewRect, self.gameLevelTipView.frame))
    {
        self.gameLevelTipView.frame = gameLevelTipViewRect;
        [self.gameLevelTipView refreshUI];
    }
    
    if (!CGRectEqualToRect(gameLevelMainViewRect, self.gameLevelMainView.frame))
    {
        self.gameLevelMainView.frame = gameLevelMainViewRect;
        [self.gameLevelMainView refreshUI];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    self.gameLevelTipView = nil;
    self.gameLevelMainView = nil;
    [super dealloc];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self resetAllSubViewFrame];
}

- (void)goToBack
{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)startGameAtLevel:(int)level
{
    GameMainViewController *gameMainVC = [[GameMainViewController alloc]init];
    gameMainVC.level = level;
    gameMainVC.gameMode = self.gameMode;
    [self.navigationController pushViewController:gameMainVC animated:NO];
    [gameMainVC release];
}

@end
